package cn.iocoder.yudao.module.reading.convert.recordings;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.reading.controller.app.recordings.vo.*;
import cn.iocoder.yudao.module.reading.dal.dataobject.recording.UserRecordingDO;
import cn.iocoder.yudao.module.reading.dal.dataobject.book.PictureBookDO;
import cn.iocoder.yudao.module.reading.dal.dataobject.book.BookPageDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

/**
 * 配音管理 Convert
 *
 * @author 李晨
 */
@Mapper
public interface RecordingsConvert {

    RecordingsConvert INSTANCE = Mappers.getMapper(RecordingsConvert.class);

    /**
     * 转换为用户配音响应 VO
     *
     * @param recordingDO 配音DO
     * @param bookDO      绘本DO
     * @return 用户配音响应 VO
     */
    default UserRecordingRespVO convertToUserRecordingRespVO(UserRecordingDO recordingDO, PictureBookDO bookDO) {
        UserRecordingRespVO vo = BeanUtils.toBean(recordingDO, UserRecordingRespVO.class);

        if (bookDO != null) {
            vo.setBookTitle(bookDO.getTitle());
            // 注意：这里不转换URL，因为在Service层已经处理了
            vo.setCoverUrl(bookDO.getCover());
        }

        // 设置音频URL（注意：这里不转换URL，因为在Service层已经处理了）
        vo.setAudioUrl(recordingDO.getAudioUrl());

        // 设置评分（使用系统评分）
        if (recordingDO.getScore() != null) {
            vo.setScore(recordingDO.getScore());
        }

        // 设置录制时间
        vo.setRecordTime(recordingDO.getCreateTime());

        // 设置是否可分享
        vo.setIsShareable(recordingDO.getIsShareable());
        
        // 设置时长（格式化为MM:SS）
        if (recordingDO.getDuration() != null) {
            int minutes = recordingDO.getDuration() / 60;
            int seconds = recordingDO.getDuration() % 60;
            vo.setDuration(String.format("%02d:%02d", minutes, seconds));
        }
        
        return vo;
    }

    /**
     * 转换为用户录制音频详情响应 VO
     *
     * @param recordingDO 配音DO
     * @return 用户录制音频详情响应 VO
     */
    default UserRecordingDetailRespVO convertToDetailRespVO(UserRecordingDO recordingDO) {
        UserRecordingDetailRespVO vo = BeanUtils.toBean(recordingDO, UserRecordingDetailRespVO.class);
        
        // 设置评分（使用系统评分）
        if (recordingDO.getScore() != null) {
            vo.setScore(recordingDO.getScore());
        }

        // 设置录制质量
        vo.setRecordingQuality(recordingDO.getRecordingQuality() != null ? recordingDO.getRecordingQuality() : "medium");
        
        // 设置创建时间
        vo.setCreatedAt(recordingDO.getCreateTime());
        
        return vo;
    }

    /**
     * 转换为配音DO
     *
     * @param reqVO 创建请求VO
     * @return 配音DO
     */
    default UserRecordingDO convertToUserRecordingDO(UserRecordingCreateReqVO reqVO) {
        UserRecordingDO recordingDO = BeanUtils.toBean(reqVO, UserRecordingDO.class);

        // 设置默认值
        recordingDO.setIsShareable(false);
        recordingDO.setScore(85); // 默认评分85分
        recordingDO.setRecordingQuality("medium"); // 默认中等质量

        return recordingDO;
    }

    /**
     * 转换为聚合配音响应 VO
     *
     * @param book       绘本DO
     * @param recordings 配音记录列表
     * @return 聚合配音响应 VO
     */
    default UserRecordingAggregateRespVO convertToAggregateRespVO(PictureBookDO book, List<UserRecordingDO> recordings) {
        UserRecordingAggregateRespVO vo = new UserRecordingAggregateRespVO();

        // 基本信息
        vo.setBookId(book.getId());
        vo.setBookTitle(book.getTitle());
        // 注意：这里不转换URL，因为在Service层已经处理了
        vo.setCoverUrl(book.getCover());
        vo.setTotalPages(book.getPages());

        // 统计信息
        vo.setRecordingCount(recordings.size());

        // 计算总时长
        int totalDuration = recordings.stream()
                .mapToInt(r -> r.getDuration() != null ? r.getDuration() : 0)
                .sum();
        vo.setTotalDuration(totalDuration);

        // 格式化总时长
        int minutes = totalDuration / 60;
        int seconds = totalDuration % 60;
        vo.setFormattedTotalDuration(String.format("%02d:%02d", minutes, seconds));

        return vo;
    }

    /**
     * 转换为配音详情页响应 VO
     *
     * @param book      绘本DO
     * @param bookPages 绘本页面列表
     * @param recordings 配音记录Map（pageId -> recording）
     * @return 配音详情页响应 VO
     */
    default UserRecordingDetailPageRespVO convertToDetailPageRespVO(
            PictureBookDO book,
            List<BookPageDO> bookPages,
            Map<Long, UserRecordingDO> recordings) {

        UserRecordingDetailPageRespVO vo = new UserRecordingDetailPageRespVO();

        // 基本信息
        vo.setBookId(book.getId());
        vo.setBookTitle(book.getTitle());
        // 注意：这里不转换URL，因为在Service层已经处理了
        vo.setCoverUrl(book.getCover());
        vo.setTotalPages(book.getPages());

        return vo;
    }

}
